(function(){
    angular.module('app').controller("agbBuyCtrl",function($scope,proxy){
        $scope.typeList = [
            {name:"全部资源类型"},
            {id:"pdb",name:"保价保量"},
            {id:"pd",name:"保价不保量"},
        ]
        $scope.stateList = [{name:'全部状态'},{id:'10',name:'待执行'},{id:'20',name:'执行中'},{id:'30',name:'结束'},{id:'40',name:'售罄'}]

        $scope.filter = {
            self:"1",
        }
        $scope.chooseDate = function (start, end) {
            $scope.filter.startDate = start;
            $scope.filter.endDate = end;
            getList();
        }
        $scope.selectType = function (item) {
            $scope.filter.skuType = item.id;
            getList();
        }
        $scope.selectState = function(item) {
            $scope.filter.executeState = item.id;
            getList();
        }

        // 下载报表
        $scope.download = function() {
            var data = {
                'page': "1",
                'size': "99999",
                "sort": ["cTime,1"],
                'filter': $scope.filter,
            };
            proxy.proxy({
                url: STATIC_URL.domian+'/dsp/sku/show/list',
                data: data,
                load:true,
                success: function(res){
                    var arr = res.data.data || [];
                    arr.forEach(function (v) {
                        v.skuType = v.skuType == 'pd'?'报价不保量':'保价保量';
                        var dateStr = "",spaceStr = "";
                        v.date = v.date || [];
                        v.date.forEach(function (data) {
                            dateStr += data.startDate + ' - ' + data.endDate + ";";
                        })
                        v.spaceInfo.forEach(function (space) {
                            spaceStr += space.appName + "，" + space.adxSpaceName + ";";
                        })
                        v.date = dateStr;
                        v.spaceInfo = spaceStr;
                    })
                    exportCsv({
                        title: ['合约资源名称','资源类型','投放时段','单价(元/千次曝光)','天量(千次曝光/每天)','合约资源详情'],
                        titleForKey: ['name','skuType','date','price','amount','spaceInfo'],
                        data: arr,
                    },"已购资源.csv");
                }
            })
        }
        getList();

        function getList(current, pagesize) {
            var data = {
                'page': (current || 1)+"",
                'size': (pagesize || 20)+"",
                "sort": ["cTime,1"],
                'filter': $scope.filter,
            };
            proxy.proxy({
                url: STATIC_URL.domian+'/dsp/sku/show/list',
                data: data,
                load:true,
                success: function(res){
                    var arr = res.data.data || [];
                    arr.forEach(function (v) {
                        v.skuType = v.skuType == 'pd'?'报价不保量':'保价保量';
                        var dateStr = "",spaceStr = "";
                        v.date = v.date || [];
                        v.date.forEach(function (data) {
                            dateStr += data.startDate + ' - ' + data.endDate + ";";
                        })
                        v.spaceInfo.forEach(function (space) {
                            spaceStr += space.appName + " - " + space.adxSpaceName + ";";
                        })
                        v.date = dateStr;
                        v.spaceInfo = spaceStr;
                    })
                    // 获得列表
                    $scope.list = arr;

                    $("#pageToolbar").html("");
                    $('#pageToolbar').Paging({
                        pagesize: pagesize || '20',
                        current: current || '1',
                        count:res.data.total,
                        toolbar:true,
                        'hash': false,
                        callback:getList
                    });
                }
            })
        }
    })
})();